package org.liang.common.process;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 程序执行器.
 * 
 * <pre>
 * 通过本对象对exe,bat进行调用执行
 * </pre>
 * 
 * @author L.Yang
 * @version v1.0 2016年1月6日 下午2:43:19
 */
public class ProcessExecutor {

    /** 命令 名. */
    private String command;

    /** 命令库. */
    private String lib;

    /** 命令所需参数. */
    private List<String> args;

    public ProcessExecutor() {
        args = new ArrayList<>();
    }

    /**
     * 构造方法.
     * 
     * @param lib
     *            命令库
     * @param command
     *            命令
     */
    public ProcessExecutor(String lib, String command) {
        super();
        this.command = command;
        this.lib = lib;
        this.args = new ArrayList<>();
    }

    /**
     * 获取 command.
     * 
     * @return 返回 command
     */
    public String getCommand() {
        return command;
    }

    /**
     * 设置 command.
     * 
     * @param command
     *            the command to set
     */
    public ProcessExecutor setCommand(String command) {
        this.command = command;
        return this;
    }

    /**
     * 获取 lib.
     * 
     * @return 返回 lib
     */
    public String getLib() {
        return lib;
    }

    /**
     * 设置 lib.
     * 
     * @param lib
     *            the lib to set
     */
    public ProcessExecutor setLib(String lib) {
        this.lib = lib;
        return this;
    }

    /**
     * 获取 args.
     * 
     * @return 返回 args
     */
    public List<String> getArgs() {
        return args;
    }

    /**
     * 设置 args.
     * 
     * @param args
     *            the args to set
     */
    public ProcessExecutor setArgs(List<String> args) {
        this.args = args;
        return this;
    }

    /**
     * 添加参数 args.
     * 
     * @param param
     *            the param to add
     */
    public ProcessExecutor addArgs(String param) {
        this.args.add(param);
        return this;
    }

    /**
     * 添加参数 args.
     * 
     * @param args
     *            the args to add
     */
    public ProcessExecutor addArgs(String[] args) {
        this.args.addAll(Arrays.asList(args));
        return this;
    }

    /**
     * 执行命令.
     * 
     * @return 命令 返回值
     */
    public int execute() {
        int result = -1;
        try {
            StringBuilder command = new StringBuilder();
            command.append(lib).append(File.separator).append(this.command);
            for (String param : args) {
                command.append(" ").append(param);
            }
            Process process = Runtime.getRuntime().exec(command.toString());
            StreamGobbler errorGobbler = new StreamGobbler(process.getErrorStream(), "ERROR");
            errorGobbler.start();
            StreamGobbler outGobbler = new StreamGobbler(process.getInputStream(), "STDOUT");
            outGobbler.start();
            result = process.waitFor();
            if (result != 0) {
                process.destroy();
            }
        } catch (Exception e) {
            result = -1;
        }
        return result;
    }
}
